﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using MyGeneration.dOOdads;

namespace WebUI.Controls
{
    public partial class StockTransferCtrl : BaseEditControl
    {
        public delegate void TransferFinished (Object sender, ListItemEventArgs e);
        private TransferFinished onTransferFinished;

        public TransferFinished OnTransferFinished
        {
            get { return onTransferFinished; }
            set { onTransferFinished = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            txtIMEI.Focus();
            if (!IsPostBack)
            {
                txtIMEI.MaxLength = SysVars.IMEILength;
                StockLocation stocks = StockLocation.GetAllActiceStockLocation();
                Utilities.LoadDropDownListNoSelect(ddlLocation, stocks.DefaultView, StockLocation.ColumnNames.LocationName, StockLocation.ColumnNames.ID);
            }
        }

        protected void btnAddIMEI_Click(object sender, EventArgs e)
        {
            AddIMEI(txtIMEI.Text);
        }

        public void AddIMEI(string imei)
        {
            if (imei.Length == SysVars.IMEILength)
            {
                if (ProductDetails.IsIMEIAvailable(imei) && lstIMEI.Items.FindByValue(imei) == null)
                {
                    ListItem item = new ListItem(imei, imei);
                    lstIMEI.Items.Add(item);
                    txtIMEI.Text = "";
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("focusIMEI();", 1), true);
                }
                else
                {
                    txtIMEI.Text = "";
                    ShowErrorMessage(mainPanel, "(*) IMEI " + imei + " has been added or not available!");
                }
            }
        }

        public void AddProductDetails(int prodDetailID)
        {
            ProductDetails prod = new ProductDetails();
            prod.LoadByPrimaryKey(prodDetailID);

            AddIMEI(prod.IMEI);
        }

        protected void btnTransfer_Click(Object sender, EventArgs e)
        {
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
            try
            {
                tx.BeginTransaction();
                String IMEIs = "";
                for (int i = 0; i < lstIMEI.Items.Count; i++)
                {
                    if (i < lstIMEI.Items.Count - 1)
                        IMEIs += lstIMEI.Items[i] + ",";
                    else
                        IMEIs += lstIMEI.Items[i];
                }

                ProductDetails p = ProductDetails.GetProductDetailByIMEIs(IMEIs);
                ProductDetailHistories pHist = new ProductDetailHistories();
                int selectedLocationID = int.Parse(ddlLocation.SelectedValue);
                for (int i = 0; i < p.RowCount; i++)
                {
                    if (p.StockLocationID != selectedLocationID)
                    {
                        Products prod = new Products();
                        prod.LoadByPrimaryKey(p.ProductID);
                        pHist.AddNew();
                        pHist.AddInfo(p.ID, p.ProductStatusID, selectedLocationID, String.Format("Transfered to {0}", ddlLocation.SelectedItem.Text), p.SupplierID, prod.Cost);
                        p.StockLocationID = selectedLocationID;
                        //p.LastModifiedByUser
                        p.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                    }
                    p.MoveNext();
                }
                p.Save();
                pHist.Save();
                tx.CommitTransaction();

                ShowHintMessage(mainPanel, "(*) All items have been transfered");

                if (onTransferFinished != null)
                {
                    ListItemEventArgs arg = new ListItemEventArgs();
                    arg.EventName = "TransferFinished";

                    onTransferFinished(this, arg);
                }
            }
            catch (Exception ex)
            {
                tx.RollbackTransaction();
                ErrorLog.WriteToFile("StockTransferControl", "btnTransfer_Click", ex.StackTrace);
                ShowErrorMessage(mainPanel, "(*) Error when transfer product items: " + ex.Message);
            }
        }

        public void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstIMEI.SelectedIndex >= 0)
                lstIMEI.Items.RemoveAt(lstIMEI.SelectedIndex);
        }

        public void btnClear_Click(object sender, EventArgs e)
        {
            lstIMEI.Items.Clear();
        }
    }
}